package com.lhq.controller;

import com.lhq.entity.User;
import com.lhq.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName UserController
 * @Description:类描述
 * @Author Tedu
 * @Date 2021/7/10 22:10
 * @Version V1.0
 **/
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

        /*
         用户注册
         */
        @RequestMapping("register")
        public String register(User user){
            try {
                userService.register(user);
                return "redirect:/login.jsp";
            } catch (Exception e) {
                e.printStackTrace();
                return "redirect:/register.jsp";
            }
        }
    /*
     * 退出登录
    */
    @RequestMapping("logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout(); //退出用户
        return "redirect:/login.jsp";
    }
    @RequestMapping("login")
    public String login(String username, String password) {
        //获取主体对象
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(username, password));
            return "redirect:/index.jsp";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名错误");
        }
        return "redirect:/login.jsp";
    }
}
